package com.mp.approvalreq.servlet.other.module;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mp.approvalreq.entity.ApprovalCost;
import com.mp.approvalreq.service.SelectApprovalServicePrx;
import com.mp.approvalreq.servlet.AbstractServlet;
import com.mp.approvalreq.util.ListHelper;
import com.mp.approvalreq.util.ServiceUtils;
import com.mp.approvalreq.util.ValidateUtil;
import com.mp.user.entity.User;
import com.mp.user.service.UserServicePrx;
import com.plusmoney.exception.RpcServerLogicalException;
import com.plusmoney.message.Message;
import com.plusmoney.message.SuccessMsg;
import com.plusmoney.util.ParameterUtils;
import com.plusmoney.util.Utils;
import com.qq.tars.client.util.ServantUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by liuqi on 2018/10/15.
 */
@WebServlet("/getApprovalCost")
public class GetApprovalCost extends AbstractServlet {
	private static final Logger logger = LoggerFactory.getLogger(GetApprovalCost.class);

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		try {
			User user = ServiceUtils.getUserInfoByRequest(req);
			Integer companyId = ParameterUtils.getIntegerParameter(req, "companyId", user.getCompanyId());
			String content = getContent(req);
			logger.info("content:{}", content);
			JSONObject jsonObject = JSON.parseObject(content);
			String requestIds = jsonObject.getString("requestIds");
			check(requestIds);

			SelectApprovalServicePrx selectApprovalService = ServiceUtils.getSelectApprovalServicePrx();
			List<ApprovalCost> approvalCosts = selectApprovalService.getApprovalCost(companyId, requestIds);

			List<ApprovalCost> list = filterUselessApprovalCost(approvalCosts);
			logger.info(list.toString());
			setApplicantName(list, companyId);

			Message.printJson(resp, SuccessMsg.getDefaultMsg(list));
		} catch (Throwable e) {
			onThrowable(req, resp, e);
		}
	}

	private void setApplicantName(List<ApprovalCost> list, Integer companyId) throws Throwable {
		if (ListHelper.isNullEmpty(list)) {
			return;
		}

		UserServicePrx userService = ServantUtils.getObject(UserServicePrx.class);
		List<Integer> applicants = new ArrayList<>(list.size());
		for (ApprovalCost approvalCost : list) {
			applicants.add(approvalCost.getApplicant());
		}

		List<User> users = userService.getUserBaseInfoListByContactIds(companyId, ListHelper.list2string(applicants));
		for (User user : users) {
			for (ApprovalCost approvalCost : list) {
				if (approvalCost.getApplicant().equals(user.getContactId())) {
					approvalCost.setApplicantName(user.getContactName());
				}
			}
		}
	}

	private void check(String requestIds) {
		for (String s : requestIds.split(",")) {
			if (!ValidateUtil.isInteger(s)) {
				throw new RpcServerLogicalException(1, "参数错误");
			}
		}
	}

	private List<ApprovalCost> filterUselessApprovalCost(List<ApprovalCost> approvalCosts) {
		List<ApprovalCost> list = new ArrayList<>(approvalCosts.size());

		for (ApprovalCost approvalCost : approvalCosts) {
			if (Utils.isNotNull(approvalCost.getCostMoney())) {
				list.add(approvalCost);
			}
		}

		return list;
	}
}
